package com.youi.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.dao.UserorderrecordDao;
import com.youi.entity.Goods;
import com.youi.entity.Goodtype;
import com.youi.entity.Groupleader;
import com.youi.entity.Leaderfan;
import com.youi.entity.Leaderstore;
import com.youi.entity.Userwechat;
import com.youi.entity.Yibaopayment;
import com.youi.entity.vo.Userorderrecord;
import com.youi.service.GoodsService;
import com.youi.service.GoodtypeService;
import com.youi.service.GroupleaderService;
import com.youi.service.LeaderfanService;
import com.youi.service.LeaderstoreService;
import com.youi.service.UserorderrecordService;
import com.youi.service.UserwechatService;
import com.youi.service.YibaopaymentService;
import com.youi.util.DateUtil;
import com.youi.util.FileUploadUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.SmsUtil;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

@Service
public class UserorderrecordServiceImpl implements UserorderrecordService {

	@Resource
	private UserorderrecordDao userorderrecordDao;
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private GoodtypeService goodtypeService;
	@Autowired
	private UserwechatService userwechatService;
	@Autowired
	private LeaderfanService leaderfanService;
	@Autowired
	private GroupleaderService groupleaderService;
	@Autowired
	private LeaderstoreService leaderstoreService;
	@Autowired
	private YibaopaymentService yibaopaymentService;
	
	@Override
	public List<Userorderrecord> getUserorderrecordList(Long merchantId,String start, String end, String merWithStart,
			String merWithEnd, String platWithStart, String platWithEnd, String orderNo, String merchantPms,
			String goodsPms, String goodsIds, String phone, Long leaderId, String receStart, String receEnd,
			String unfreezeStart, String unfreezeEnd, Integer status,Integer platformStatus, Integer merchantStatus, Integer pageIndex, Integer pageNum) {
		// TODO Auto-generated method stub
		return userorderrecordDao.getUserorderrecordList(merchantId,start, end, merWithStart, merWithEnd, platWithStart, platWithEnd, orderNo, 
				merchantPms, goodsPms, goodsIds, phone, leaderId, receStart, receEnd, unfreezeStart, unfreezeEnd, status,platformStatus,merchantStatus, pageIndex, pageNum);
	}


	@Override
	public Integer getUserorderrecordCount(Long merchantId,String start, String end, String merWithStart, String merWithEnd,
			String platWithStart, String platWithEnd, String orderNo, String merchantPms, String goodsPms,
			String goodsIds, String phone, Long leaderId, String receStart, String receEnd, String unfreezeStart,
			String unfreezeEnd, Integer status,Integer platformStatus, Integer merchantStatus) {
		// TODO Auto-generated method stub
		return userorderrecordDao.getUserorderrecordCount(merchantId,start, end, merWithStart, merWithEnd, platWithStart, platWithEnd, orderNo, 
			merchantPms, goodsPms, goodsIds, phone, leaderId, receStart, receEnd, unfreezeStart, unfreezeEnd, status, platformStatus, merchantStatus);
	}


	@Override
	public String getGoodsIds(String goosTypeNo) {
		// TODO Auto-generated method stub
		if (goosTypeNo!=null&&goosTypeNo.trim().length()>0) {
			List<Long> goodsIdList=new ArrayList<Long>();
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			prams.put("code", goosTypeNo);
			Goodtype goodtype=goodtypeService.getByParam(prams);
			if (goodtype!=null) {
				prams=new HashMap<String, Object>();
				prams.put(Common.DELSTATUS, DelStatus.NODEL);
				prams.put("goodsTypeId", goodtype.getId());
				List<Goods> goodsList=goodsService.getByParams(prams);
				if (goodsList!=null&&goodsList.size()>0) {
					for (Goods goods : goodsList) {
						goodsIdList.add(goods.getId());
					}
					String goodsIds=ObjectUtil.listToString(goodsIdList);
					return goodsIds;
				}
			}
		}
		return null;
	}


	@Override
	public void getUserorderrecordMsg(List<Userorderrecord> userorderrecordList) {
		// TODO Auto-generated method stub
		if (userorderrecordList!=null&&userorderrecordList.size()>0) {
			for (Userorderrecord userorderrecord : userorderrecordList) {
				//判断用户身份获取门店信息
				Integer identity=checkUserIidentity(userorderrecord.getUserId());
				if (identity==1) {
					getAuthStoreMsgByFan(userorderrecord);
				}else if (identity==2) {
					getAuthStoreMsgByLeader(userorderrecord);
				}
				
				//获取主订单号
				Map<String, Object> prams=new HashMap<String, Object>();
				prams.put(Common.DELSTATUS, DelStatus.NODEL);
				Map<String, Object> searchParams=new HashMap<String, Object>();
				searchParams.put("orderNos", userorderrecord.getOrderNo());
				Yibaopayment yibaopayment=yibaopaymentService.getByParam(prams, searchParams, null);
				if (yibaopayment!=null) {
					userorderrecord.setTopOrderNo(yibaopayment.getSerialNumber());
				}
				
				//获取分类编码
				prams=new HashMap<String, Object>();
				prams.put(Common.DELSTATUS, DelStatus.NODEL);
				prams.put("goodsNo", userorderrecord.getGoodsNo());
				Goods goods=goodsService.getByParam(prams);
				if (goods!=null) {
					Goodtype goodtype=goodtypeService.get(goods.getGoodsTypeId());
					if (goodtype!=null) {
						userorderrecord.setGoodsTypeNo(goodtype.getCode());
					}
				}
			}
		}
	}

	/**
	 * 检测订单用户身份
	 * @param userId
	 * @return 0非粉丝 1粉丝 2团长 
	 */
	private Integer checkUserIidentity(Long userId) {
		Map<String, Object> prams=new HashMap<String, Object>();
		prams.put(Common.DELSTATUS, DelStatus.NODEL);
		prams.put(Common.USERID, userId);		
		Userwechat userother=userwechatService.getByParam(prams);
		if (userother!=null) {
			prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);	
			prams.put("unionid", userother.getUnionid());
			Leaderfan leaderfan=leaderfanService.getByParam(prams);
			if (leaderfan!=null) {
				return 1;
			}
			prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			prams.put(Common.USERID, userId);
			Groupleader groupleader=groupleaderService.getByParam(prams);
			if (groupleader!=null) {
				return 2;
			}
		}
		return 0;		
	}
	
	/**
	 * 获取粉丝所属门店信息
	 * @param userorderrecord
	 */
	private void getAuthStoreMsgByFan(Userorderrecord userorderrecord) {
		Userwechat userwechat=userwechatService.get(userorderrecord.getUserId());
		if (userwechat!=null) {
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);	
			prams.put("unionid", userwechat.getUnionid());
			Leaderfan leaderfan=leaderfanService.getByParam(prams);
			if (leaderfan!=null) {
				prams=new HashMap<String, Object>();
				prams.put(Common.DELSTATUS, DelStatus.NODEL);
				prams.put(Common.ID, leaderfan.getLeaderId());
				Groupleader groupleader=groupleaderService.getByParam(prams);
				if (groupleader!=null) {
					prams=new HashMap<String, Object>();
					prams.put(Common.DELSTATUS, DelStatus.NODEL);
					prams.put("leaderId", groupleader.getId());
					Map<String, Object> sortParams=new HashMap<String, Object>();
					sortParams.put(Common.ID, Common.DESC);
					Leaderstore leaderstore=leaderstoreService.getByParam(prams, null, sortParams);
					if (leaderstore!=null) {
						userorderrecord.setAuthStore(leaderstore.getAuthStore());
						userorderrecord.setAuthStoreCode(leaderstore.getAuthStoreCode());
					}
				}
			}
		}
	}
	
	/**
	 * 获取团长所属门店信息
	 * @param userorderrecord
	 */
	private void getAuthStoreMsgByLeader(Userorderrecord userorderrecord) {
		Map<String, Object> prams=new HashMap<String, Object>();
		prams.put(Common.DELSTATUS, DelStatus.NODEL);	
		prams.put(Common.USERID, userorderrecord.getUserId());
		Groupleader groupleader=groupleaderService.getByParam(prams);
		if (groupleader!=null) {
			prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			prams.put("leaderId", groupleader.getId());
			Map<String, Object> sortParams=new HashMap<String, Object>();
			sortParams.put(Common.ID, Common.DESC);
			Leaderstore leaderstore=leaderstoreService.getByParam(prams, null, sortParams);
			if(leaderstore!=null) {
				userorderrecord.setAuthStore(leaderstore.getAuthStore());
				userorderrecord.setAuthStoreCode(leaderstore.getAuthStoreCode());
			}
		}
	}


	@Override
	public String toExcelUserorderrecord(HttpServletRequest request, List<Userorderrecord> userorderrecordList) {
		// TODO Auto-generated method stub				
		try {
			String fileName="merchantwithdraw"+new Date().getTime()+""+SmsUtil.getRandom(3)+".xls";//文件名				
			String fileUrl=FileUploadUtil.getUploadUrl(request, "filedownload"+File.separator+fileName);//生成路径
			WritableWorkbook workbook = Workbook.createWorkbook(new File(fileUrl));
			WritableSheet sheet1=workbook.createSheet("Sheet1", 0);
			WritableFont font=new WritableFont(WritableFont.createFont("微软雅黑"),10);
			WritableCellFormat format=new WritableCellFormat();
			format.setAlignment(Alignment.CENTRE);
			format.setVerticalAlignment(VerticalAlignment.CENTRE);
			Label label=new Label(0, 0, "主订单号", format);
			sheet1.addCell(label);
			label=new Label(1, 0, "子订单号", format);
			sheet1.addCell(label);
			label=new Label(2, 0, "下单时间", format);
			sheet1.addCell(label);
			label=new Label(3, 0, "供应商编号", format);
			sheet1.addCell(label);
			label=new Label(4, 0, "供应商名称", format);
			sheet1.addCell(label);
			label=new Label(5, 0, "商品名称", format);
			sheet1.addCell(label);
			label=new Label(6, 0, "商品代码", format);
			sheet1.addCell(label);
			label=new Label(7, 0, "分类编码", format);
			sheet1.addCell(label);
			label=new Label(8, 0, "规格", format);
			sheet1.addCell(label);
			label=new Label(9, 0, "单价", format);
			sheet1.addCell(label);
			label=new Label(10, 0, "数量", format);
			sheet1.addCell(label);
			label=new Label(11, 0, "实付金额", format);
			sheet1.addCell(label);
			label=new Label(12, 0, "顾客名称", format);
			sheet1.addCell(label);
			label=new Label(13, 0, "顾客手机号", format);
			sheet1.addCell(label);
			label=new Label(14, 0, "配送方式", format);
			sheet1.addCell(label);
			label=new Label(15, 0, "快递公司", format);
			sheet1.addCell(label);
			label=new Label(16, 0, "快递单号", format);
			sheet1.addCell(label);
			label=new Label(17, 0, "收货地址", format);
			sheet1.addCell(label);
			label=new Label(18, 0, "确认收货时间", format);
			sheet1.addCell(label);
			label=new Label(19, 0, "订单解冻时间", format);
			sheet1.addCell(label);
			label=new Label(20, 0, "供应商提现单号", format);
			sheet1.addCell(label);
			label=new Label(21, 0, "平台提现单号", format);
			sheet1.addCell(label);
			label=new Label(22, 0, "认证门店号", format);
			sheet1.addCell(label);
			label=new Label(23, 0, "门店名称", format);
			sheet1.addCell(label);
			label=new Label(24, 0, "一级佳选官姓名", format);
			sheet1.addCell(label);
			label=new Label(25, 0, "一级佳选官分润", format);
			sheet1.addCell(label);			
			label=new Label(26, 0, "二级佳选官姓名", format);
			sheet1.addCell(label);
			label=new Label(27, 0, "二级佳选官分润", format);
			sheet1.addCell(label);
			label=new Label(28, 0, "平台佣金", format);
			sheet1.addCell(label);
			label=new Label(29, 0, "平台分润状态", format);
			sheet1.addCell(label);
			label=new Label(30, 0, "平台佣金提现成功时间", format);
			sheet1.addCell(label);
			label=new Label(31, 0, "供应商佣金", format);
			sheet1.addCell(label);
			label=new Label(32, 0, "供应商佣金提现状态", format);
			sheet1.addCell(label);
			label=new Label(33, 0, "供应商提现成功时间", format);
			sheet1.addCell(label);
			Userorderrecord userorderrecord=null;
			for (int i = 0,j=1; i < userorderrecordList.size(); i++,j++) {
				userorderrecord=userorderrecordList.get(i);
				ObjectUtil.ToChangeNullToEmpty(userorderrecord);
				label=new Label(0, j, userorderrecord.getTopOrderNo(), format);
				sheet1.addCell(label);
				label=new Label(1, j, userorderrecord.getOrderNo(), format);
				sheet1.addCell(label);
				label=new Label(2, j, DateUtil.simpdfyMdHms.format(userorderrecord.getGmtOrder()), format);
				sheet1.addCell(label);
				label=new Label(3, j, userorderrecord.getMerchantCode(), format);
				sheet1.addCell(label);
				label=new Label(4, j, userorderrecord.getMerchantName(), format);
				sheet1.addCell(label);
				label=new Label(5, j, userorderrecord.getGoodsName(), format);
				sheet1.addCell(label);
				label=new Label(6, j, userorderrecord.getGoodsNo(), format);
				sheet1.addCell(label);
				label=new Label(7, j, userorderrecord.getGoodsTypeNo(), format);
				sheet1.addCell(label);
				label=new Label(8, j, userorderrecord.getSpecification(), format);
				sheet1.addCell(label);
				label=new Label(9, j, String.valueOf(userorderrecord.getGoodsPrice()), format);
				sheet1.addCell(label);
				label=new Label(10, j, String.valueOf(userorderrecord.getGoodsCount()), format);
				sheet1.addCell(label);
				label=new Label(11, j, String.valueOf(userorderrecord.getPayableAmount()), format);
				sheet1.addCell(label);
				label=new Label(12, j, userorderrecord.getConsignee(), format);
				sheet1.addCell(label);
				label=new Label(13, j, userorderrecord.getPhone(), format);
				sheet1.addCell(label);
				label=new Label(14, j, userorderrecord.getDeliveryType(), format);
				sheet1.addCell(label);
				label=new Label(15, j, userorderrecord.getExpress(), format);
				sheet1.addCell(label);
				label=new Label(16, j, userorderrecord.getExpressCode(), format);
				sheet1.addCell(label);
				label=new Label(17, j, userorderrecord.getAddress(), format);
				sheet1.addCell(label);
				if (userorderrecord.getGmtReceiving()!=null) {
					label=new Label(18, j, DateUtil.simpdfyMdHms.format(userorderrecord.getGmtReceiving()), format);
					sheet1.addCell(label);
				}
				if (userorderrecord.getGmtUnfreeze()!=null) {
					label=new Label(19, j, DateUtil.simpdfyMdHms.format(userorderrecord.getGmtUnfreeze()), format);
					sheet1.addCell(label);
				}
				if(userorderrecord.getMerWithdrawNo()!=null) {
					label=new Label(20, j, userorderrecord.getMerWithdrawNo(), format);
					sheet1.addCell(label);
				}
				if (userorderrecord.getPlamtwithdrawNo()!=null) {
					label=new Label(21, j, userorderrecord.getPlamtwithdrawNo(), format);
					sheet1.addCell(label);
				}
				label=new Label(22, j, userorderrecord.getAuthStoreCode(), format);
				sheet1.addCell(label);
				label=new Label(23, j, userorderrecord.getAuthStore(), format);
				sheet1.addCell(label);
				label=new Label(24, j, userorderrecord.getTopLeader(), format);
				sheet1.addCell(label);
				label=new Label(25, j, String.valueOf(userorderrecord.getTopLeaderAnt()), format);
				sheet1.addCell(label);
				label=new Label(26, j, userorderrecord.getSecondLeader(), format);
				sheet1.addCell(label);
				label=new Label(27, j, String.valueOf(userorderrecord.getSecondLeaderAnt()), format);
				sheet1.addCell(label);
				label=new Label(28, j, String.valueOf(userorderrecord.getPlatAmount()), format);
				sheet1.addCell(label);
				label=new Label(29, j, ObjectUtil.getMerchantStatus(userorderrecord.getPlatformStatus()), format);
				sheet1.addCell(label);
				if(userorderrecord.getGmtPlatformWith()!=null) {
					label=new Label(30, j, "平台佣金提现成功时间", format);
					sheet1.addCell(label);
				}
				label=new Label(31, j, String.valueOf(userorderrecord.getMerAmount()), format);
				sheet1.addCell(label);
				label=new Label(32, j, ObjectUtil.getMerchantStatus(userorderrecord.getMerchantStatus()), format);
				sheet1.addCell(label);
				if(userorderrecord.getGmtMerchantWith()!=null) {
					label=new Label(33, j, DateUtil.simpdfyMdHms.format(userorderrecord.getGmtMerchantWith()), format);
					sheet1.addCell(label);
				}
			}
			workbook.write();
			workbook.close();
			fileUrl="filedownload/"+fileName;
			return fileUrl;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return null;
	}
}
